//  -------------------- iSharePoint -------------------- //
// 
//  Company:	IT Complex, LLC
//  Project:	Smart.Common
//  File name:	NamedEventHandlerInfo.cs
//  Developer:	Solomatov Igor
//  Created:	15.04.2012
// 
//  -------------------- iSharePoint -------------------- //

using System;

namespace Smart.Common
{
	public class NamedEventHandlerInfo
	{
		public Delegate Handler;

		public int Priority;

		public int AddIdx;

		public NamedEventHandlerInfo()
		{
		}

		public NamedEventHandlerInfo(Delegate handler, int priority, int addIdx)
		{
			Handler = handler;
			Priority = priority;
			AddIdx = addIdx;
		}

		public bool FireEvent<TArg1>(string eventName, TArg1 arg1)
		{
			if (Handler == null)
				return true;

			//H.LT(" >> FireEvent " + Handler.Method.Name + ", " + eventName);

			var res = true;

			try
			{
				res = ((NamedEventHandler<TArg1>)Handler)(eventName, arg1);
			}
			catch (Exception x)
			{
				H.LX("NamedEventHandlerInfo.FireEvent", "NamedEventHandlerInfo.FireEvent method: " + Handler.Method.Name + ", eventName: " + eventName, x);
			}

			//H.LT(" << FireEvent " + Handler.Method.Name + ", " + eventName);

			return res;
		}

		public bool FireEvent<TArg1, TArg2>(string eventName, TArg1 arg1, TArg2 arg2)
		{
			if (Handler == null)
				return true;

			//H.LT(" >> FireEvent " + Handler.Method.Name + ", " + eventName);

			var res = true;

			try
			{
				res = ((NamedEventHandler<TArg1, TArg2>)Handler)(eventName, arg1, arg2);
			}
			catch (Exception x)
			{
				H.LX("NamedEventHandlerInfo.FireEvent", "NamedEventHandlerInfo.FireEvent method: " + Handler.Method.Name + ", eventName: " + eventName, x);
			}

			//H.LT(" << FireEvent " + Handler.Method.Name + ", " + eventName);

			return res;
		}

		public bool FireEvent<TArg1, TArg2, TArg3>(string eventName, TArg1 arg1, TArg2 arg2, TArg3 arg3)
		{
			if (Handler == null)
				return true;

			//H.LT(" >> FireEvent " + Handler.Method.Name + ", " + eventName);

			var res = true;

			try
			{
				res = ((NamedEventHandler<TArg1, TArg2, TArg3>)Handler)(eventName, arg1, arg2, arg3);
			}
			catch (Exception x)
			{
				H.LX("NamedEventHandlerInfo.FireEvent", "NamedEventHandlerInfo.FireEvent method: " + Handler.Method.Name + ", eventName: " + eventName, x);
			}

			//H.LT(" << FireEvent " + Handler.Method.Name + ", " + eventName);

			return res;
		}

		public bool FireEvent<TArg1, TArg2, TArg3, TArg4>(string eventName, TArg1 arg1, TArg2 arg2, TArg3 arg3, TArg4 arg4)
		{
			if (Handler == null)
				return true;

			//H.LT(" >> FireEvent " + Handler.Method.Name + ", " + eventName);

			var res = true;

			try
			{
				res = ((NamedEventHandler<TArg1, TArg2, TArg3, TArg4>)Handler)(eventName, arg1, arg2, arg3, arg4);
			}
			catch (Exception x)
			{
				H.LX("NamedEventHandlerInfo.FireEvent", "NamedEventHandlerInfo.FireEvent method: " + Handler.Method.Name + ", eventName: " + eventName, x);
			}

			//H.LT(" << FireEvent " + Handler.Method.Name + ", " + eventName);

			return res;
		}
	}
}